package com.danger.Activity;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.List;

public class NewsAdapter extends BaseAdapter implements AbsListView.OnScrollListener {

    private List<NewsBean> mList;
    private LayoutInflater inflater;
    private Context context;
    private ImageLoader imageLoader;
    private int start;
    private int end;
    public static String[] urls;
    private ListView listView;
    private boolean firstIn;

    public NewsAdapter(List<NewsBean> mList, Context context,ListView listView) {
        this.listView = listView;
        this.mList = mList;
        this.context = context;
        inflater = inflater.from(context);
        imageLoader = new ImageLoader(listView);
        urls = new String[mList.size()];
        for (int i = 0; i < mList.size(); i++) {
            urls[i] = mList.get(i).getNewsIconUrl();
        }
        firstIn = true;
        this.listView.setOnScrollListener(this);
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_layout, null);
            holder = new Holder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }
        NewsBean bean = mList.get(position);
        String url = bean.getNewsIconUrl();
        holder.ic_icon.setTag(url);
        holder.ic_icon.setImageResource(R.mipmap.ic_launcher);
//        ImageLoader.getInstance().loadImageByThread(holder.ic_icon, url);
        imageLoader.loadImageByAsyncTask(holder.ic_icon, url);

        holder.tv_title.setText(bean.getNewsTitle());
        holder.tv_content.setText(bean.getNewsContent());

        return convertView;
    }

    /**
     * 滑动状态切换才调用
     *
     * @param view
     * @param scrollState
     */
    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        //当前状态处于停止状态，加载可见项
        if (scrollState == SCROLL_STATE_IDLE) {
            imageLoader.loadImages(start,end);
        } else {    //如果是其他状态则停止任务
            imageLoader.cancelAllTask();
        }
    }

    /**
     * 整个滑动过程中都会调用
     *
     * @param view
     * @param firstVisibleItem
     * @param visibleItemCount
     * @param totalItemCount
     */
    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        //获取第一个可见项的position
        start = firstVisibleItem;
        //获取最后一个可见项的position
        end = firstVisibleItem + visibleItemCount;
        //第一次嫌显示时候调用
        if(firstIn && visibleItemCount > 0){
            imageLoader.loadImages(start,end);
            firstIn = false;
        }
    }

    class Holder {
        private ImageView ic_icon;
        private TextView tv_title;
        private TextView tv_content;

        public Holder(View view) {
            tv_title = (TextView) view.findViewById(R.id.tv_title);
            tv_content = (TextView) view.findViewById(R.id.tv_content);
            ic_icon = (ImageView) view.findViewById(R.id.ic_icon);
        }
    }

}
